package com.cqc.palette01;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.graphics.Palette;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ImageView iv;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
        tv = (TextView) findViewById(R.id.tv);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

        //同步
        Palette palette = Palette.from(bitmap).generate();
        initColor(palette);


        //异步
//        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
//            @Override
//            public void onGenerated(Palette palette) {
//                 initColor(palette);
//            }
//        });

        //Swatch： 样品Vibrant:有活力的；Dark:暗淡的；Muted:温和的；Light:明亮的；Dominant：主要的
        List<Palette.Swatch> swatches = palette.getSwatches();
        Palette.Swatch dominantSwatch = palette.getDominantSwatch();
        Palette.Swatch mutedSwatch = palette.getMutedSwatch();
        Palette.Swatch vibrantSwatch = palette.getVibrantSwatch();
        Palette.Swatch darkMutedSwatch = palette.getDarkMutedSwatch();
        Palette.Swatch darkVibrantSwatch = palette.getDarkVibrantSwatch();
        Palette.Swatch lightMutedSwatch = palette.getLightMutedSwatch();
        Palette.Swatch lightVibrantSwatch = palette.getLightVibrantSwatch();



    }

    private void initColor(Palette palette) {
        Palette.Swatch swatch = palette.getVibrantSwatch();
        int rgb = swatch.getRgb();
        int titleTextColor = swatch.getTitleTextColor();
        tv.setBackgroundColor(rgb);
        tv.setTextColor(titleTextColor);
    }
}
